<?php

require_once 'IService.class.php';

abstract class AbstractService implements IService {
    
    /* Atributo de log para todas as classes filhas. */
    protected $log;

    //Model Object
    private $model;
    
    //Connection Database Object
    protected $conn;

    /**
     * Default constructor.
     */
    public function __construct() {
        $this->log = new Log();
    }

    protected function getConn() {
        return $this->conn;
    }

    public function getModel() {
        return $this->model;
    }

    public function setModel($model) {
        $this->model = $model;
    }

    /**
     * Start here some process.
     * @param $_GET, $_POST or $_REQUEST $objRequest
     */
    protected function init($connType) {
        if($this->log->isDebugEnabled()) {
            $this->log->logDebug('Entrou init. Abrindo conexao: ' . (IConnection::CONN_BUS == $connType ? 'Business' : 'Admin'));
        }
        
        //Creating connection obj
        if(IConnection::CONN_BUS == $connType) {
            $this->conn = ConnectionFactory::getBusinessConnection($_SESSION[EMPRESA]);
        } else if(IConnection::CONN_ADM == $connType) {
            $this->conn = ConnectionFactory::getAdminConnection($_SESSION[EMPRESA]);
        }
        //Openning the connection
        $this->conn->open();
    }

    /**
     * End here some process.
     * @param $_GET, $_POST or $_REQUEST $objRequest
     */
    protected function end() {
        //Closing the connection
        $this->conn->close();
    }

    /**
     * This method is implemented in AbstractController.
     * It is calling the other 3 methods.
     * If you want to subscribe, don't forget it.
     * init($objRequest);
     * specificExecute($objRequest);
     * end($objRequest);
     * @param $_GET, $_POST or $_REQUEST $objRequest
     */
    public final function execute($connType = IConnection::CONN_BUS) {
        if($this->log->isDebugEnabled()) {
            $this->log->logDebug('Executando o método execute()');
        }
        
        $this->init($connType);
        $this->specificExecute();
        $this->end();
    }

    /**
     * Executa as chamadas Dao para manipular o banco de dados.
     * @author Tiago <tiago.professor@gmail.com>
     */
    protected function specificExecute() {
        if($this->log->isDebugEnabled()) {
            $this->log->logDebug('Executando o método specificExecute() - Acao ' . $this->getModel()->getAction());
        }
        
        switch ($this->getModel()->getAction()) {
            case self::SALVAR:
                $this->executarSalvar();
                break;
            case self::SALVAR_LIMPAR:
                $this->executarSalvar();
                break;
            case self::SALVAR_COMO:
                $this->executarSalvarComo();
                break;
            case self::PESQUISAR:
                $this->executarPesquisa();
                break;
            case self::EXCLUIR:
                $this->executarExcluir();
                break;
        }
    }

    /**
     * Gera um array com as linhas retornadas no result.
     * @author Tiago <tiago.professor@gmail.com>
     * @return array
     */
    protected function getArrayFromResult($result) {
        $arrayRows = array();
        while ($row = $this->conn->fetchAssoc($result)) {
            $arrayRows[] = $row;
        }
        return $arrayRows;
    }

}

?>
